package com.xiaoq.parking.endpoint;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.xiaoq.coms.GenericDataResponse;
import com.xiaoq.coms.GenericListResponse;
import com.xiaoq.coms.GenericResponse;
import com.xiaoq.coms.util.IdGenerator;
import com.xiaoq.parking.entity.ParkingEntity;
import com.xiaoq.parking.entity.ParkingRecordEntity;
import com.xiaoq.parking.service.IParkingRecordService;
import com.xiaoq.parking.vo.ParkingRecordVo;
import com.xiaoq.parking.vo.ParkingVo;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;

@RestController
@RequestMapping("/api/v1/parking_record")
@Api(tags={"停车信息相关接口"})
public class ParkingRecordEndpoint {
	
	@Autowired
	private IdGenerator idGenerator;
	
	@Autowired
	private IParkingRecordService parkRecSer;
	
	public static final class ParkingRecordVoListResponse extends GenericListResponse<ParkingRecordVo> {}
	
	public static final class ParkingRecordVoDetailResponse extends GenericDataResponse<ParkingRecordVo> {}
	
	@ApiOperation(value = "添加停车信息", notes = "添加停车信息")
	@ApiResponses({ @ApiResponse(code = 200, response = GenericResponse.class, message = "添加停车信息") })
	@PostMapping(value = "/", produces = { MediaType.APPLICATION_JSON_UTF8_VALUE })
	public ResponseEntity<?> addParkingRec(
			@RequestBody @ApiParam(value = "parkRecVo", required = true) ParkingRecordVo parkRecVo,HttpServletRequest request) {
				
		String userId = (String) request.getAttribute("userId");
		ParkingRecordEntity parkingRec = new ParkingRecordEntity();
		BeanUtils.copyProperties(parkRecVo, parkingRec);
		parkingRec.setId(idGenerator.nextId());
		parkingRec.setUserId(userId);
		parkingRec.setIsParking("00");
		parkRecSer.addParkingRecord(parkingRec);
		return GenericResponse.ok();

	}
	
	@ApiOperation(value = "修改停车信息(将原有数据和修改后的数据都传过来了)", notes = "修改停车信息")
	@ApiResponses({ @ApiResponse(code = 200, response = GenericResponse.class, message = "修改停车信息") })
	@PutMapping(value = "/", produces = { MediaType.APPLICATION_JSON_UTF8_VALUE })
	public ResponseEntity<?> editParkingRec(
			@RequestBody @ApiParam(value = "parkRecVo", required = true) ParkingRecordVo parkRecVo) {
		ParkingRecordEntity parkingRec = new ParkingRecordEntity();
		BeanUtils.copyProperties(parkRecVo, parkingRec);
		parkRecSer.updateParkingRecord(parkingRec);
		return GenericResponse.ok();
	}
	
	@ApiOperation(value = "停止停车信息", notes = "停止停车信息")
	@ApiResponses({ @ApiResponse(code = 200, response = GenericResponse.class, message = "停止停车信息") })
	@PutMapping(value = "/stop", produces = { MediaType.APPLICATION_JSON_UTF8_VALUE })
	public ResponseEntity<?> stopParkingRec(@RequestParam(value="parkingRecId",required = true)String parkingRecId) {
		parkRecSer.deleteParkingRecord(parkingRecId);
		return GenericResponse.ok();
	}
	
	@ApiOperation(value = "停车信息列表", notes = "停车信息列表")
	@ApiResponses({ @ApiResponse(code = 200, response = ParkingRecordVoListResponse.class, message = "停车信息列表") })
	@GetMapping(value = "/list", produces = { MediaType.APPLICATION_JSON_UTF8_VALUE })
	public ResponseEntity<?> parkingRecList(HttpServletRequest request) {
		String userId = (String) request.getAttribute("userId");
		List<ParkingRecordEntity> parkingRecList = parkRecSer.parkingRecordList(userId);
		List<ParkingRecordVo> parkingRecVoList = new ArrayList<>();
		ParkingRecordVo parkingRecVo = null;
		for(ParkingRecordEntity parkingRec:parkingRecList){
			parkingRecVo = new ParkingRecordVo();
			BeanUtils.copyProperties(parkingRec, parkingRecVo);
			parkingRecVoList.add(parkingRecVo);
		}
		return ParkingRecordVoListResponse.listNoCount(parkingRecVoList);
		
		
		

	}
	
	@ApiOperation(value = "根据id获取停车信息详情", notes = "根据id获取停车信息详情")
	@ApiResponses({ @ApiResponse(code = 200, response = ParkingRecordVoDetailResponse.class, message = "根据id获取停车信息详情") })
	@GetMapping(value = "/detail", produces = { MediaType.APPLICATION_JSON_UTF8_VALUE })
	public ResponseEntity<?> parkingDetail(@RequestParam(value="id")String id) {
		ParkingRecordEntity parkingRec = parkRecSer.parkingRecordDetail(id);
		ParkingRecordVo parkingRecVo = new ParkingRecordVo();
		BeanUtils.copyProperties(parkingRec, parkingRecVo);
		return ParkingRecordVoDetailResponse.okWithData(parkingRecVo);
		
		

	}

}
